package engine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class LoadProgram {
	public static InstructionSet instructionset;
	public static ArrayList<String> list;
	public LoadProgram(int basePC) {
		instructionset=new InstructionSet();
		instructionset.basePC=basePC;
		list=new ArrayList<String>();
	}
	public static void loadContents(String file) throws IOException {
		BufferedReader br=new BufferedReader(new FileReader(new File(file)));
		while(br.ready()) {
			String line=br.readLine();
			String[] data=line.split(" ");
			String opcode=data[0];
			list.add(line.trim());
			Instruction inst=new Instruction();
			if(opcode.equalsIgnoreCase("ADD")||opcode.equalsIgnoreCase("NAND")
					||opcode.equalsIgnoreCase("MUL")||opcode.equalsIgnoreCase("DIV")) {
				inst=new Instruction(5, opcode);
				String rd=(data[1].substring(0, data[1].indexOf(',')));
				String rs=(data[2].substring(0, data[2].indexOf(',')));
				String rt=data[3].trim();
				inst.rd=rd;
				inst.rs=rs;
				inst.rt=rt;
			}
			if(opcode.equalsIgnoreCase("ADDI")) {
				inst=new Instruction(5, opcode);
				String rd=(data[1].substring(0, data[1].indexOf(',')));
				String rs=(data[2].substring(0, data[2].indexOf(',')));
				String imm=data[3].trim();
				inst.rd=rd;
				inst.rs=rs;
				inst.imm=imm;
			}
			if(opcode.equalsIgnoreCase("LW")) {
				inst=new Instruction(1, opcode);
				String rt=(data[1].substring(0, data[1].indexOf(',')));
				String rs=(data[2].substring(0, data[2].indexOf(',')));
				String imm=data[3];
				inst.rt=rt;
				inst.rs=rs;
				inst.imm=imm;
			}
			if(opcode.equalsIgnoreCase("SW")) {
				inst=new Instruction(2, opcode);
				String rs=(data[1].substring(0, data[1].indexOf(',')));
				String rt=(data[2].substring(0, data[2].indexOf(',')));
				String imm=data[3];
				inst.rs=rs;
				inst.rt=rt;
				inst.imm=imm;
			}
			if(opcode.equalsIgnoreCase("JMP")) {
				inst=new Instruction(3, opcode);
				String rs=(data[1].substring(0, data[1].indexOf(',')));
				String imm=data[2];
				inst.rs=rs;
				inst.imm=imm;
			}
			if(opcode.equalsIgnoreCase("BEQ")) {
				inst=new Instruction(4, opcode);
				String rs=(data[1].substring(0, data[1].indexOf(',')));
				String rt=(data[2].substring(0, data[2].indexOf(',')));
				String imm=data[3];
				inst.rs=rs;
				inst.imm=imm;
				inst.rt=rt;
			}
			if(opcode.equalsIgnoreCase("JALR")) {
				inst=new Instruction(6, opcode);
				String rd=(data[1].substring(0, data[1].indexOf(',')));
				String rs=data[2];
				inst.rd=rd;
				inst.rs=rs;
			}
			if(opcode.equalsIgnoreCase("RET")) {
				inst=new Instruction(7, opcode);
				String rs=data[1];
				inst.rs=rs;
			}
			if(line.trim().equals("END")) {
				inst=new Instruction(10, line.trim());
			}
			instructionset.addInstruction(inst);
		}
	}
	public Instruction decode(String line) {
		String[] data=line.split(" ");
		String opcode=data[0];
		list.add(line.trim());
		Instruction inst=new Instruction();
		if(opcode.equalsIgnoreCase("ADD")||opcode.equalsIgnoreCase("NAND")
				||opcode.equalsIgnoreCase("MUL")||opcode.equalsIgnoreCase("DIV")) {
			inst=new Instruction(5, opcode);
			String rd=(data[1].substring(0, data[1].indexOf(',')));
			String rs=(data[2].substring(0, data[2].indexOf(',')));
			String rt=data[3].trim();
			inst.rd=rd;
			inst.rs=rs;
			inst.rt=rt;
		}
		if(opcode.equalsIgnoreCase("ADDI")) {
			inst=new Instruction(5, opcode);
			String rd=(data[1].substring(0, data[1].indexOf(',')));
			String rs=(data[2].substring(0, data[2].indexOf(',')));
			String imm=data[3].trim();
			inst.rd=rd;
			inst.rs=rs;
			inst.imm=imm;
		}
		if(opcode.equalsIgnoreCase("LW")) {
			inst=new Instruction(1, opcode);
			String rt=(data[1].substring(0, data[1].indexOf(',')));
			String rs=(data[2].substring(0, data[2].indexOf(',')));
			String imm=data[3];
			inst.rt=rt;
			inst.rs=rs;
			inst.imm=imm;
		}
		if(opcode.equalsIgnoreCase("SW")) {
			inst=new Instruction(2, opcode);
			String rs=(data[1].substring(0, data[1].indexOf(',')));
			String rt=(data[2].substring(0, data[2].indexOf(',')));
			String imm=data[3];
			inst.rs=rs;
			inst.rt=rt;
			inst.imm=imm;
		}
		if(opcode.equalsIgnoreCase("JMP")) {
			inst=new Instruction(3, opcode);
			String rs=(data[1].substring(0, data[1].indexOf(',')));
			String imm=data[2];
			inst.rs=rs;
			inst.imm=imm;
		}
		if(opcode.equalsIgnoreCase("BEQ")) {
			inst=new Instruction(4, opcode);
			String rs=(data[1].substring(0, data[1].indexOf(',')));
			String rt=(data[2].substring(0, data[2].indexOf(',')));
			String imm=data[3];
			inst.rs=rs;
			inst.imm=imm;
			inst.rt=rt;
		}
		if(opcode.equalsIgnoreCase("JALR")) {
			inst=new Instruction(6, opcode);
			String rd=(data[1].substring(0, data[1].indexOf(',')));
			String rs=data[2];
			inst.rd=rd;
			inst.rs=rs;
		}
		if(opcode.equalsIgnoreCase("RET")) {
			inst=new Instruction(7, opcode);
			String rs=data[1];
			inst.rs=rs;
		}
		if(line.trim().equals("END")) {
			inst=new Instruction(10, line.trim());
		}
		inst.instString=line.trim();
		return inst;
	}
}
